package net.yxsoft.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.upload.UploadFile;
import rapid.core.ContextStateEnum;
import rapid.core.TResult;
import rapid.jfinal.ParamKeyMap;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

public class ApiBaseController extends Controller {

  private boolean isReadData = false;
  private String readData = "";

  protected TResult result = new TResult();

  public ApiBaseController() {
  }

  @Override
  public String getPara() {
    if ("application/json".equals(getRequest().getContentType())
            || "text/json".equals(getRequest().getContentType())) {
      if(!isReadData){
        readData = HttpKit.readData(getRequest());
        isReadData = true;
      }
      return readData;
    }
    return super.getPara();
  }

  @Override
  public String getPara(String name) {
    String ww=getRequest().getContentType();
    if ("application/json".equals(getRequest().getContentType())
            || "text/json".equals(getRequest().getContentType())) {
      return JSONObject.parseObject(getPara()).getString(name);
    }
    return super.getPara(name);
  }

  @Override
  public Map<String, String[]> getParaMap(){

    if ("application/json".equals(getRequest().getContentType())
            || "text/json".equals(getRequest().getContentType())) {

      return JSONObject.parseObject(getPara(), Map.class);
    }
    return super.getParaMap();
  }

  public ParamKeyMap getParamKeyMap() {
    return ParamKeyMap.by(this);
  }

  public void renderEmpty() {
    this.renderJson(new Object());
  }

  @Override
  public void renderFile(File file) {
    this.getResponse().reset();
    super.renderFile(file);
  }

  public void webUploader() {
    this.renderJson(this.uploadFile().getData());
  }

  protected TResult uploadFile() {
    UploadFile file = this.getFile();
    if (file != null) {
      this.result.setState(ContextStateEnum.SUCCESS);
      this.result.setData("/files/" + file.getFileName());
      this.result.setMessage("上传成功。");
    } else {
      this.result.setState(ContextStateEnum.FAIL);
      this.result.setMessage("上传失败。");
    }

    return this.result;
  }

  protected TResult none() {
    return TResult.normal();
  }

  protected TResult fail() {
    return TResult.fail("操作失败。");
  }

  protected TResult fail(Object data) {
    return TResult.fail("操作失败。").data(data);
  }

  protected TResult success() {
    return TResult.success("操作成功。");
  }

  protected TResult success(Object data) {
    return TResult.success("操作成功。").data(data);
  }

  protected String getCurrUid() {
    return (String)this.getSessionAttr("uid");
  }

  protected boolean setSubViewId() {
    String viewId = this.getPara("viewId", (String)null);
    if (viewId == null) {
      viewId = this.getPara("viewid", (String)null);
      if (viewId == null) {
        this.renderJson(new TResult(ContextStateEnum.EXCEPTION, "请求子页时没有附加viewId信息。"));
        return false;
      }
    }

    this.setAttr("viewId", viewId);
    return true;
  }

  protected void renderSubView(String fileName) {
    this.renderSubView("subview/", fileName);
  }

  protected void renderSubView(String subViewRootPath, String fileName) {
    if (this.setSubViewId()) {
      if (!fileName.contains(".")) {
        fileName = fileName + ".html";
      }

      int endIndex = subViewRootPath.length() - 1;
      if (subViewRootPath.lastIndexOf("/") != endIndex) {
        subViewRootPath = subViewRootPath + "/";
      }

      if (fileName.contains("/")) {
        fileName = fileName.replace("/", "");
      }

      this.render(subViewRootPath + fileName);
    }

  }

  protected <T> ArrayList<T> getModels(Class<T> cls, String paraName, int size) {
    ArrayList<T> rs = new ArrayList();

    for(int i = 0; i < size; ++i) {
      rs.add(this.getModel(cls, paraName + "[" + i + "]"));
    }

    return rs;
  }

  protected <T> ArrayList<T> getBeans(Class<T> cls, String paraName, int size) {
    ArrayList<T> rs = new ArrayList();

    for(int i = 0; i < size; ++i) {
      rs.add(this.getBean(cls, paraName + "[" + i + "]"));
    }

    return rs;
  }
}
